﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.DAO;

namespace SCRUM.Core.BusinessObjects
{
    /// <summary>
    ///  Business object for user story.
    /// </summary>
    [Serializable()]
    public class UserStory : PriorityBusinessObject
    {
        /// <summary>
        /// Gets or sets the sprint ID.
        /// </summary>
        /// <value>The sprint ID.</value>
        public int SprintID { get; set; }

        /// <summary>
        /// Gets or sets the type. 1 = functional, 2=technical, 3=other
        /// </summary>
        /// <value>The type.</value>
        public int Type { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserStory"/> class.
        /// </summary>
        /// <param name="id">ID of the user story.</param>
        /// <param name="sprintID">The sprint ID.</param>
        /// <param name="name">Name of the user story.</param>
        /// <param name="description">Description of US.</param>
        /// <param name="estimation">The estimation.</param>
        /// <param name="state">Current state of US.</param>
        /// <param name="progress">The progress.</param>
        /// <param name="priority">The priority.</param>
        /// <param name="change">The change.</param>
        public UserStory(int id, int sprintID, string name, string description, double estimation, int stateID, int type, double progress, int priority, DateTime change)
            : base(id, name, description, estimation, progress, priority, change, stateID)
        {
            this.SprintID = sprintID;
            this.Type = type;
            this.stateType = State.StateTypes.User_Story;
        }

        /// <summary>
        /// Gets or sets the system ID - object's unique identifier in scrum methodology system (derived from ID)
        /// </summary>
        /// <value>The system ID.</value>
        public override string SystemID
        {
            get
            {
                return MakeSystemID("U",this.ID);
            }
            set {}
        }

        /// <summary>
        /// Gets or sets the superior item's ID.
        /// </summary>
        /// <value>The superior item ID.</value>
        public override int SuperiorItemID
        {
            get { return this.SprintID == null ? -1 : (int)this.SprintID; }
            set { this.SprintID = value; }
        }

        /// <summary>
        /// Gets or sets the sprint.
        /// </summary>
        /// <value>The sprint.</value>
        public Sprint Sprint
        {
            get
            {
                // Lazy loading
                if (this.sprint == null)
                {
                    //this.sprint = DAOSprint.GetSprintByUserStoryID(this.ID);
                    this.sprint = DAOSprint.GetSprintById((int)SprintID);
                }
                return this.sprint;
            }
            set
            {
                this.sprint = value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the sprint.
        /// </summary>
        /// <value>The name of the sprint.</value>
        public string SprintName 
        { 
            get
            {
                Sprint s = this.Sprint;
                return s == null ? "" : s.Name;
            }
            set
            {
            }
        }

        /// <summary>
        /// Gets or sets the attachments.
        /// </summary>
        /// <value>The attachments.</value>
        public IList<Attachment> Attachments
        {
            get
            {
                // Lazy loading
                if (this.attachments == null)
                {
                    this.attachments = DAOAttachment.GetAttachmetnsForUserStory(this);
                }
                return this.attachments;
            }
            set
            {
                this.attachments = value;
            }
        }

        /// <summary>
        /// Gets or sets the backlog items.
        /// </summary>
        /// <value>The backlog items.</value>
        public IList<BLI> BacklogItems
        {
            get
            {
                // Lazy loading
                if (this.backlogItems == null)
                {
                    this.backlogItems = DAO.DAOBli.GetAllBlisForUserStory(this.ID);
                }
                return this.backlogItems;
            }
            set
            {
                this.backlogItems = value;
            }
        }

        /// <summary>
        /// Sprint that contains this user story.
        /// </summary>
        private Sprint sprint;

        /// <summary>
        /// Attachments of this user story.
        /// </summary>
        private IList<Attachment> attachments;

        /// <summary>
        /// Backlog items associated to this user story.
        /// </summary>
        private IList<BLI> backlogItems;

        public override string ToString()
        {
            return this.Name;
        }
    }
}
